package handler

import (
	"chat/api/request"
	"chat/globar"
	"chat/intermal/dao"
	"chat/intermal/model"
	"github.com/gin-gonic/gin"
	"net/http"
)

func UserAdd(c *gin.Context) {
	var req request.UseraddReq
	var err error
	if err = c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"Code":    http.StatusOK,
			"message": "验证失败",
			"data":    false,
		})
		return
	}
	var useradd model.Useradd
	dao.GetOneFile(&model.Useradd{Name: req.Name}, &useradd)
	useradd = model.Useradd{
		UserId: int64(c.GetUint("userId")),
		Name:   req.Name,
		Info:   req.Info,
		Age:    req.Age,
		Sex:    req.Sex,
	}
	dao.Create(&useradd)
	if useradd.Id == 0 {
		c.JSON(http.StatusOK, gin.H{
			"Code":    http.StatusOK,
			"message": "添加好友失败",
			"data":    false,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"Code":    http.StatusOK,
		"message": "添加成功",
		"data":    useradd.Id,
	})
	return
}

func UserList(c *gin.Context) {
	var useradd []model.Useradd
	dao.GetFile(&useradd)
	c.JSON(http.StatusOK, gin.H{
		"Code":    http.StatusOK,
		"message": "查看成功",
		"data":    useradd,
	})

}

func UserLike(c *gin.Context) {
	var req request.UseraddReq
	var useradd model.Useradd
	err := globar.DB.Where("name like ?", "%"+req.Name+"").Find(&useradd).Error
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"Code":    http.StatusOK,
			"message": "查看失败",
			"data":    false,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"Code":    http.StatusOK,
		"message": "查看成功",
		"data":    useradd,
	})
	return
}
